﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace YXShop.Web.admin.store
{
    public partial class storeask_edit : System.Web.UI.Page
    {
        YXShop.BLL.Store.StoreAsk Bll = new YXShop.BLL.Store.StoreAsk();
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                int id = ChangeHope.WebPage.PageRequest.GetQueryInt("id");
                InitWebControl();
                if (id > 0)
                {
                    YXShop.Common.PromptInfo.Popedom("014005004");
                    GetStoreAsk(id);
                }
                else
                {
                    YXShop.Common.PromptInfo.Popedom("014005002");
                }
            }
        }

        private void GetStoreAsk(int id)
        {
            YXShop.Model.Store.StoreAsk s = new Model.Store.StoreAsk();
            YXShop.BLL.Admin.Administrators admin = new BLL.Admin.Administrators();
            s = Bll.GetModelById(id);
            txtMessageTitle.Text = s.MessageTitle;

            foreach (ListItem item in ddlreceiveType.Items)
            {
                if (item.Value == s.ReceiveType.ToString())
                {
                    item.Selected = true;
                    break;
                }
            }
            txtMessage.Value = s.Message;
            if (s.ReceiveType.Value == 1)
            {
                trStore.Visible = false;
                trStoreCenter.Visible = false;
            }
            else if (s.ReceiveType.Value == 2)
            {
                trStore.Visible = false;
                trStoreCenter.Visible = true;
                hfstorecenterid.Value = s.ReceiveStoreCenterId.ToString();
                YXShop.BLL.Store.StoreCenter scBLL = new BLL.Store.StoreCenter();
                txtStoreCenter.Text = scBLL.GetModelById(s.ReceiveStoreCenterId.Value).StoreCentername;
            }
            else if (s.ReceiveType.Value == 3)
            {
                trStore.Visible = true;
                trStoreCenter.Visible = false;
                hfstoreid.Value = s.ReceiveStoreId.ToString();
                YXShop.BLL.Store.Store sBLL = new BLL.Store.Store();
                txtStore.Text = sBLL.GetModelById(s.ReceiveStoreId.Value).Storename;
            }
        }

        #region 验证
        private void InitWebControl()
        {
            YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();

            ChangeHope.WebPage.WebControl.Validate(this.txtMessageTitle, "消息标题不能为空", "isnull", "必填", "该项为必填项");
            this.ddlreceiveType.DataSource = YXShop.Common.ValuePair.GetStoreAskType();
            this.ddlreceiveType.DataTextField = "StoreAskTypeName";
            this.ddlreceiveType.DataValueField = "StoreAskTypeID";
            this.ddlreceiveType.DataBind();

            this.txtStoreCenter.Attributes.Add("readonly", "readonly");
            this.txtStoreCenter.Attributes.Add("onclick", "selectFile('Storecenterlist',new Array(" + this.hfstorecenterid.ClientID + "," + this.txtStoreCenter.ClientID + "),310,450,'" + sp.DummyPaht + "');");

            this.txtStore.Attributes.Add("readonly", "readonly");
            this.txtStore.Attributes.Add("onclick", "selectFile('Storelist',new Array(" + this.hfstoreid.ClientID + "," + this.txtStore.ClientID + "),310,450,'" + sp.DummyPaht + "');");

        }
        #endregion

        #region 保存数据
        protected void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }


        private void Save()
        {
            int id = ChangeHope.WebPage.PageRequest.GetQueryInt("id");
            YXShop.Model.Admin.AdminInfo admin = YXShop.Common.AdministrorManager.Get();
            if (id > 0)
            {
                YXShop.Model.Store.StoreAsk ask = Bll.GetModelById(id);
                ask.MessageTitle = txtMessageTitle.Text;
                ask.Message = txtMessage.Value;
                ask.ReceiveType = Convert.ToInt32(ddlreceiveType.SelectedValue);
                if (ask.ReceiveType == 1)
                {
                    ask.ReceiveStoreCenterId = 0;
                    ask.ReceiveStoreId = 0;
                }
                else if (ask.ReceiveType == 2)
                {
                    ask.ReceiveStoreCenterId = Convert.ToInt32(hfstorecenterid.Value);
                    ask.ReceiveStoreId = 0;
                }
                else if (ask.ReceiveType == 3)
                {
                    ask.ReceiveStoreCenterId = 0;
                    ask.ReceiveStoreId = Convert.ToInt32(hfstoreid.Value);
                }
                Bll.Update(ask);
                Response.Redirect("storeask_edit.aspx?id=" + ask.Id);
            }
            else
            {
                YXShop.Model.Store.StoreAsk ask = new Model.Store.StoreAsk();
                ask.MessageTitle = txtMessageTitle.Text;
                ask.Message = txtMessage.Value;
                ask.ReceiveType = Convert.ToInt32(ddlreceiveType.SelectedValue);
                if (ask.ReceiveType == 1)
                {
                    ask.ReceiveStoreCenterId = 0;
                    ask.ReceiveStoreId = 0;
                }
                else if (ask.ReceiveType == 2)
                {
                    ask.ReceiveStoreCenterId = Convert.ToInt32(hfstorecenterid.Value);
                    ask.ReceiveStoreId = 0;
                }
                else if (ask.ReceiveType == 3)
                {
                    ask.ReceiveStoreCenterId = 0;
                    ask.ReceiveStoreId = Convert.ToInt32(hfstoreid.Value);
                }
                ask.SenderId = admin.AdminId;
                ask.SenderTime = DateTime.Now;
                ask.Answercount = 0;
                int addid=Bll.Add(ask);
                Response.Redirect("storeask_edit.aspx?id=" + addid);

            }
        }
        #endregion

        protected void ddlreceiveType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlreceiveType.SelectedValue == "1")
            {
                trStore.Visible = false;
                trStoreCenter.Visible = false;
            }
            else if (ddlreceiveType.SelectedValue == "2")
            {
                trStore.Visible = false;
                trStoreCenter.Visible = true;
            }
            else if (ddlreceiveType.SelectedValue == "3")
            {
                trStore.Visible = true;
                trStoreCenter.Visible = false;
            }
        }



    }
}
